package com.company.finance.ddd.domain.core.voucher.rule.concrete;

import com.company.finance.ddd.common.FlagEnum;
import com.company.finance.ddd.domain.core.bill.aggregate.Bill;
import com.company.finance.ddd.domain.core.voucher.enums.VoucherSubjectEnum;
import com.company.finance.ddd.domain.core.voucher.rule.*;
import com.company.finance.ddd.domain.core.bill.enums.BillSubTypeEnum;
import com.company.finance.ddd.domain.core.voucher.rule.VoucherType;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author wanglei
 * @version $$Id: PnRnPaymentRule, v 0.1 2021/2/3 14:56 wanglei Exp $$
 */
@Component
public class PaymentRule extends AbsRule {

    @Override
    protected RuleDefinition createRuleDefinition() {
        return RuleDefinition.builder().voucherType(VoucherType.builder().code("04").name("付款").build())
                .addBillSpecification(RuleDefinition.BillSpecification.builder()
                        .prepayment(FlagEnum.NO).relateDeal(FlagEnum.NO)
                        .billSubTypeEnum(BillSubTypeEnum.PURCHASE_PAYMENT).build())
                .addBillSpecification(RuleDefinition.BillSpecification.builder()
                        .prepayment(FlagEnum.NO).relateDeal(FlagEnum.NO)
                        .billSubTypeEnum(BillSubTypeEnum.SUPPLIER_EXPENSE).build())
                .addBillSpecification(RuleDefinition.BillSpecification.builder()
                        .prepayment(FlagEnum.NO).relateDeal(FlagEnum.NO)
                        .billSubTypeEnum(BillSubTypeEnum.RELATED_TRANSACTION).build())
                .build();
    }

    @Override
    public List<RuleItem> createRuleDefinitionItems(Bill bill) {
        String companyName = companyRepository.get(bill.getCOMPANY_ID()).getCOMPANY_NAME();
        String supplierName = supplierRepository.get(bill.getSUPPLIER_ID()).getSUPPLIER_NAME();

        List<RuleItem> ruleItemList = new ArrayList<>();
        ruleItemList.add(RuleItem.builder().creditSubject(VoucherSubjectEnum.BANK)
                .debitSubject(VoucherSubjectEnum.SHOULD_RECEIPT_SETTLE)
                .creditDigest(MessageFormat.format("{0}支付{1}", companyName , supplierName))
                .debitDigest(MessageFormat.format("{0}收到{1}", supplierName, companyName))
                .build());

        return ruleItemList;
    }
}
